﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Setup.Skills;
using Domain.Setup.Entities;
using Infrastructure.Common.Data;
using System.Linq.Expressions;

namespace Infrastructure.Data.Setup.Repositories
{
    /// <summary>
    /// The Repository used to interact with Skills.
    /// </summary>
    public class SkillRepository : Repository<Skill>, ISkillRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SkillRepository"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public SkillRepository(IContext context)
            : base(context)
        { }

        /// <summary>
        /// Adds the specified Skill.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public override Skill Add(Skill item)
        {
            return this.Context.Skills.Add(item);
        }

        /// <summary>
        /// Updates the specified Skill.
        /// </summary>
        /// <param name="item">The item.</param>
        public override void Update(Skill item)
        {
            //this.Context.Skills.Attach(item);
            var entry = this.Context.Skills.Find(item.Id);
            entry.Name = item.Name;
            entry.Description = item.Description;
        }

        /// <summary>
        /// Deletes the specified Skill.
        /// </summary>
        /// <param name="item">The item.</param>
        public override void Delete(Skill item)
        {
            var study = this.Context.Skills.Find(item.Id);

            this.Context.Skills.Remove(study);
        }

        /// <summary>
        /// Gets the Skills that match the specified predicates.
        /// </summary>
        /// <param name="predicates">The predicates.</param>
        /// <returns></returns>
        public override IEnumerable<Skill> Get(params Expression<Func<Skill, bool>>[] predicates)
        {
            var query = from s in this.Context.Skills
                        select s;

            if (predicates != null)
            {
                foreach (var condition in predicates)
                {
                    query = query.Where(condition);
                }
            }

            return query.ToArray();
        }
    }
}
